import tornado.web
import config
import database
import logger
from tornado.web import url,StaticFileHandler
from views import index,download
from config import settings,appconfig,options

class Application(tornado.web.Application):
	def __init__(self):
		handlers = [
			url(r'/',index.IndexHandler,name="index"), #反向解析
			(r'/add',index.AddMovieHandler),
			(r'/append', index.AppendHandler),
			(r'/author', index.AuthorHandler),
			(r'/convert',index.ConvertMovieHandler),
			(r'/customplay', index.PlayHandler),	# 边下边播
			(r'/cut', index.Home3Handler),
			(r'/cutvideo', index.CutVideoHandler),
			(r'/del', index.DelHandler),  # 从下载页删除
			(r'/deleteMovie', index.DeleteMovieHandler),  # 从home页删除电影
			(r'/delcutvideo', index.DeleteCutHandler),
			(r'/download_new', index.DownloadHandler),
			(r'/downloading/list', index.DownloadingListHandler),
			(r'/fuli', index.Home2Handler),
			(r'/getvideos', index.GetVideosHandler),
			(r'/home', index.HomeHandler),
			(r'/hls/(?P<section>\w+)/(?P<filepath>.*)$', index.Static_FileHandler, {"path": '', "default_filename": "index.html"}),	# kwargs.path会赋值给StaticFileHandler里的self.root
			(r'/Images/(.*)$', tornado.web.StaticFileHandler, {"path": appconfig['image_path']}),
			(r'/m3u8',index.PlayHandler),	# 顶部播放链接
			(r'/play/(?P<section>\w+)$', index.PlayHandler),	# 播放页面
			(r'/playvideo',index.PlayVideoHandler),		# 播放数据
			(r'/play_d/(?P<section>\w+)/(?P<index>\w+)/hls/(?P<filepath>.*)$', index.Static_FileHandler,{"path": '', "default_filename": "index.html"}),  # 边下边播，链接来自index文件
			(r'/redownload', index.ReDownloadHandler),
			(r'/search(.*)',index.SearchHandler),
			(r'/stop',index.StopHandler),
			(r'/switchMovie',index.SwitchHandler),
			(r'/switchArea', index.AreaHandler),
			(r'/websocket',index.MessageHandler),
			(r'/websocket/(?P<id>.+)',index.MessageHandler),
			(r'/uploader/file',index.UploaderFileHandler,{"path":appconfig['image_path']}),
			(r'/.*',StaticFileHandler),
		]
		super().__init__(handlers=handlers,**settings)
		# tornado.web.Application.__init__(self, handlers, **settings)

		# 实例化数据库模块
		self.db = database.Connection(database=appconfig['database_path'])
		all_tables = self.db.query("select name from sqlite_master where type='table' order by name")

		if all_tables != None:
			if {'name': 'section'} not in all_tables:
				self.db.execute('create table section (\
					id varchar(20) primary key,\
					name varchar(1024),\
					indexname varchar(125),\
					ftype varchar(64),\
					success interger,\
					merge interger,\
					total interger,\
					done interger,\
					url varchar(65535),\
					content_length interger,\
					done_length interger,\
					cacle_path varchar(1024),\
					save_path varchar(1024),\
					addtime DATETIME\
					);')
			if {'name': 'movie'} not in all_tables:
				self.db.execute('create table movie (\
					id varchar(20) primary key,\
					name varchar(1024),\
					logo varchar(1024),\
					cuttype interger,\
					ftype varchar(64),\
					indexname varchar(4096),\
					path varchar(1024),\
					area interger,\
					addtime DATETIME\
					);')

		# 实例化下载模块
		self.download = download.Download(db=self.db,cacle_dir=appconfig['video_path'])
		print('Start web address: %s:%s'%(options.get('ip'),options.get('port')))
